package com.paomi.client.activity.person.presenter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter;
import com.hannesdorfmann.mosby3.mvp.MvpView;
import com.paomi.client.R;
import com.paomi.client.activity.person.View.CardView;
import com.paomi.client.activity.person.model.CheckCouponBean;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.net.Api;
import com.paomi.client.net.BaseCallback;
import com.paomi.client.net.Constants;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;

public class CardPre extends MvpBasePresenter<CardView> {

    /**
     * couponId	false	优惠券ID(code 为null时可选 code不为空必选)
     * assType	true	优惠券类型 1.演出 2 衍生品
     * code	false	劵码
     *
     * @param map
     */
    public void getCoupon(Map map) {
        Api.getInstanceService(Constants.User_URL).getCoupon(map).enqueue(new BaseCallback() {
            @Override
            public void onResponse(Call call, Object result) {
                getView().getCardSuccess();
            }
        });
    }

    /**
     * 显示兑换的弹出框
     * @param activity
     */
    public void showChangeDialog(Activity activity){
        LayoutInflater inflater = LayoutInflater.from(activity);
        View view = inflater.inflate(R.layout.dialog_exchange_show, null);
        final Dialog dialog = new Dialog(activity, R.style.theme_dialog);
        dialog.setContentView(view);
        dialog.show();

        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM); //可设置dialog的位置
        window.getDecorView().setPadding(0, 0, 0, 0); //消除边距
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;   //设置宽度充满屏幕
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
        EditText et_input=dialog.findViewById(R.id.et_input);
        if (dialog.isShowing()) {//显示软键盘
            activity.getWindow().getDecorView().postDelayed(new Runnable() {
                @Override
                public void run() {
                    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm != null) {
                        et_input.requestFocus();
                        imm.showSoftInput(et_input, 0);
                    }
                }
            }, 100);
        }

        TextView tv_exchange=dialog.findViewById(R.id.tv_exchange);
        tv_exchange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (et_input.getText().toString().trim().isEmpty()) {
                    return;
                }
                String code=et_input.getText().toString().trim();
                Map<String, String> map = new HashMap<>();
                map.put("assType", "1");
                if (code == null || code.equals("")) {
//            map.put("couponId",);
                } else {
                    map.put("code", code);
                }
                getCoupon(map);
            }
        });
    }


    /**
     * 检查是否已领取
     * @param couponId
     */
    public void checkConpon(String couponId) {
        Api.getInstanceService(Constants.User_URL).checkCoupon(couponId).enqueue(new BaseCallback<CheckCouponBean>() {
            @Override
            public void onResponse(Call<CheckCouponBean> call, CheckCouponBean result) {
                getView().checkSuccess(result);
            }
        });
    }
}
